using System;
using System.Reflection;
using System.Runtime.Serialization;

namespace SharpCore.NullableTypes
{
	/// <summary>
	/// Wraps the <see cref="System.Boolean"/> value type in an object so that references can be null.
	/// </summary>
	[Serializable]
	public sealed class NullableBoolean : INullable
	{
		/// <summary>
		/// The <see cref="System.Boolean"/> value this instance represents.
		/// </summary>
		private bool booleanValue;

		/// <summary>
		/// Initializes a new instance of the NullableBoolean class.
		/// </summary>
		public NullableBoolean()
		{
			this.booleanValue = false;
		}

		/// <summary>
		/// Initializes a new instance of the NullableBoolean class with a <see cref="System.Boolean"/> value.
		/// </summary>
		/// <param name="booleanValue">The <see cref="System.Boolean"/> that this instance represents.</param>
		public NullableBoolean(bool booleanValue)
		{
			this.booleanValue = booleanValue;
		}

		/// <summary>
		/// Initializes a new instance of the NullableBoolean class with a <see cref="System.String"/> that represents a <see cref="System.Boolean"/>.
		/// </summary>
		/// <param name="booleanValue">The string representation of a <see cref="System.Boolean"/> value.</param>
		public NullableBoolean(string booleanValue)
		{
			this.booleanValue = Boolean.Parse(booleanValue);
		}

		/// <summary>
		/// Creates a NullableBoolean that is serializable with the specified SerializationInfo and StreamingContext.
		/// </summary>
		/// <param name="info">A SerializationInfo object containing the information required to serialize the NullableBoolean.</param>
		/// <param name="context">A StreamingContext object containing the source and destination of the serialized stream associated with the NullableBoolean.</param>
		private NullableBoolean(SerializationInfo info, StreamingContext context)
		{
			this.booleanValue = (bool) info.GetBoolean("Value");
		}

		/// <summary>
		/// Returns the data needed to serialize the NullableBoolean.
		/// </summary>
		/// <param name="info">A SerializationInfo object containing the information required to serialize the NullableBoolean.</param>
		/// <param name="context">A StreamingContext object containing the source and destination of the serialized stream associated with the NullableBoolean.</param>
		void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("Value", booleanValue);
		}

		/// <summary>
		/// Gets or Sets the <see cref="System.Boolean"/> value this instance represents.
		/// </summary>
		public bool BooleanValue
		{
			get { return booleanValue; }
			set { booleanValue = value; }
		}

		/// <summary>
		/// Converts a <see cref="System.Boolean"/> to a NullableBoolean.
		/// </summary>
		/// <param name="booleanValue">The <see cref="System.Boolean"/> to be converted.</param>
		/// <returns>A NullableBoolean instance that wraps the <see cref="System.Boolean"/> value.</returns>
		public static implicit operator NullableBoolean(bool booleanValue)
		{
			return new NullableBoolean(booleanValue);
		}

		/// <summary>
		/// Checks two NullableBoolean instances for equality.
		/// </summary>
		/// <param name="a">A NullableBoolean instance to test for equality.</param>
		/// <param name="b">A NullableBoolean instance to test for equality.</param>
		/// <returns><b>true</b> if both instances are the same; otherwise <b>false</b>.</returns>
		public static bool operator ==(NullableBoolean a, NullableBoolean b)
		{
			if (a == null && b == null)
			{
				return true;
			}
			else if (a == null || b == null)
			{
				return false;
			}
			else
			{
				return (a.booleanValue == b.booleanValue);
			}
		}

		/// <summary>
		/// Checks two NullableBoolean instances for inequality.
		/// </summary>
		/// <param name="a">A NullableBoolean instance to test for inequality.</param>
		/// <param name="b">A NullableBoolean instance to test for inequality.</param>
		/// <returns><b>true</b> if each instance is different; otherwise <b>false</b>.</returns>
		public static bool operator !=(NullableBoolean a, NullableBoolean b)
		{
			if (a == null && b == null)
			{
				return false;
			}
			else if (a == null || b == null)
			{
				return true;
			}
			else
			{
				return (a.booleanValue != b.booleanValue);
			}
		}

		/// <summary>
		/// Compares this instance to a specified object and returns an indication of their relative values.
		/// </summary>
		/// <param name="obj">An object to compare to this instance, or a null reference.</param>
		/// <returns>A signed integer that indicates the relative order of this instance and obj.</returns>
		public int CompareTo(object obj)
		{
			if (obj == null)
			{
				return 1;
			}
			else if (obj is NullableBoolean)
			{
				NullableBoolean nullableBoolean = obj as NullableBoolean;
				return booleanValue.CompareTo(nullableBoolean.booleanValue);
			}
			else if (obj is bool)
			{
				return booleanValue.CompareTo(obj);
			}
			else
			{
				throw new ArgumentException("The specified object was neither a bool nor a NullableBool instance.");
			}
		}

		/// <summary>
		/// Returns a value indicating whether this instance is equal to a specified object.
		/// </summary>
		/// <param name="obj">An object to compare to this instance.</param>
		/// <returns><b>true</b> if obj is a NullableBoolean and has the same value as this instance; otherwise, <b>false</b>.</returns>
		public override bool Equals(object obj)
		{
			return booleanValue.Equals(obj);
		}

		/// <summary>
		/// Returns the hash code for this instance.
		/// </summary>
		/// <returns>A hash code for the current NullableBoolean.</returns>
		public override int GetHashCode()
		{
			return booleanValue.GetHashCode();
		}

		/// <summary>
		/// Converts the value of this instance to its equivalent string representation.
		/// </summary>
		/// <returns>The string representation of the value of this instance.</returns>
		public override string ToString()
		{
			return booleanValue.ToString();
		}
	}
}
